package com.chagee.channel.server.event.service.strategy.larkCallback;

import com.alibaba.fastjson.JSONObject;
import com.chagee.channel.api.bo.lark.ApprovalTaskStatusChangeCallbackBO;
import com.chagee.channel.common.constant.LarkConstant;
import com.chagee.channel.server.config.LarkConfig;
import com.chagee.channel.server.enums.SubsystemEnum;
import com.chagee.channel.server.event.service.MqSender;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: chang.jiawei
 * @date: 2024/02/19
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ApprovalTaskStatusChangeCallbackHandler implements CallbackHandler {

    private final LarkConfig larkConfig;

    private final MqSender mqSender;

    @Value("${rocketmq.producer.topic.lark-callback.approval-task-status-event}")
    private String topic;
    
    @Override
    public boolean getType(String type) {
        return LarkConstant.APPROVAL_TASK_CHANGE_CALLBACK_TYPE.equals(type);
    }

    @Override
    public void process(JSONObject eventBody) {

        ApprovalTaskStatusChangeCallbackBO approvalTaskStatusChangeCallbackBO = JSONObject.parseObject(JSONObject.toJSONString(eventBody), ApprovalTaskStatusChangeCallbackBO.class);
        SubsystemEnum subsystem = larkConfig.fetchSubsystemByApprovalCode(approvalTaskStatusChangeCallbackBO.getApprovalCode());
        if (Objects.isNull(subsystem)) {
            log.error("不需要的审批回调：{}", eventBody);
            return;
        }
        log.info("飞书回调需要发送给子系统：{}", subsystem);
        mqSender.send(approvalTaskStatusChangeCallbackBO, topic, subsystem.name());
        mqSender.sendOldMQ(approvalTaskStatusChangeCallbackBO,topic, subsystem.name());

    }
}
